#light

// val square_func : int -> int
let square_func x = x * x

// val square_cps : int -> (int -> 'a) -> 'a
let square_cps x cont = cont(x * x)

// val it : int = 16
let result = square_cps 4 (fun x -> x)

// val length_cps : 'a list -> int
let length_cps lst =
    let rec length_cont l cont =
        match l with
        | []   -> cont 0
        | _::t -> length_cont t (fun x -> cont(1 + x))
    length_cont lst (fun x -> x)

// val it : int = 5
let result2 = length_cps [1..5]

// val fib_cps : int -> int
let fib_cps n =
    let rec fib_cont a cont =
        if a <= 2 then cont 1
        else
            fib_cont (a - 2) (fun x ->
                fib_cont (a - 1) (fun y ->
                    cont(x + y)))
    fib_cont n (fun x -> x)

// val it : int = 55
let result3 = fib_cps 10
